package com.xxhui.animatordemo;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void click(View v) {
        switch (v.getId()) {
            case R.id.translation:
                ObjectAnimator animator = ObjectAnimator.ofFloat(v, "translationX", 300);
                animator.setDuration(300);
                animator.start();
                break;
            case R.id.value_holder:
                PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("translationX", 300F);
                PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f);
                PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f);
                ObjectAnimator.ofPropertyValuesHolder(v, holder1, holder2, holder3).setDuration(1000).start();
                break;
            case R.id.animator_set:
                ObjectAnimator animator1 = ObjectAnimator.ofFloat(v, "translationX", 300);
                ObjectAnimator animator2 = ObjectAnimator.ofFloat(v, "scaleX", 1f, 0, 1f);
                ObjectAnimator animator3 = ObjectAnimator.ofFloat(v, "scaleY", 1f, 0, 1f);
                AnimatorSet set = new AnimatorSet();
                set.setDuration(1000);
                set.playTogether(animator1, animator2, animator3);
                set.start();
                break;
            case R.id.animator:
                Animator animatorI = AnimatorInflater.loadAnimator(this, R.animator.xml_animator);
                animatorI.setTarget(v);
                animatorI.start();
                break;
            case R.id.animate:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    v.animate().alpha(89).x(300f).withStartAction(new Runnable() {
                        @Override
                        public void run() {

                        }
                    }).withEndAction(new Runnable() {
                        @Override
                        public void run() {

                        }
                    }).start();
                } else {
                    v.animate().alpha(89).x(300f).start();
                }
                break;
            case R.id.addBut:
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                Button button = new Button(this);
                button.setText("new");
                LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
                //设置过度动画
                ScaleAnimation animation = new ScaleAnimation(0,1,0,1);
                animation.setDuration(200);
                //设置布局动画的显示属性
                LayoutAnimationController controller = new LayoutAnimationController(animation,0.5f);
                controller.setOrder(LayoutAnimationController.ORDER_RANDOM);
                //为ViewGroup设置布局动画
                layout.setLayoutAnimation(controller);

                button.setLayoutParams(params);
                layout.addView(button);
                break;
            default:
                break;
        }
    }


}
